const { exec } = require("./index");

exports.addAddress = async function ({
  tel,
  nickname,
  provice_city_area,
  detail_address,
  uid,
  longitude,
  latitude,
}) {
  const sql = `INSERT INTO address (id, tel, nickname, provice_city_area, detail_address, uid, longitude, latitude) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?);`;
  try {
    const result = await exec(sql, [
      tel,
      nickname,
      provice_city_area,
      detail_address,
      uid,
      longitude,
      latitude,
    ]);
    return result;
  } catch (error) {
    console.log("数据库操作报错", error);
    return error;
  }
};

exports.getAddress = async function ({ uid }) {
  const sql = `select * from address where uid = ?`;
  try {
    const result = await exec(sql, [uid]);
    return result;
  } catch (error) {
    console.log("数据库操作报错", error);
    return error;
  }
};

exports.getAddressByID = async function ({ id }) {
  const sql = `select * from address where id = ?`;
  try {
    const result = await exec(sql, [id]);
    return result;
  } catch (error) {
    console.log("数据库操作报错", error);
    return error;
  }
};

exports.delAddressByID = async function ({ id, uid }) {
  const sql = `delete from address where id = ? and uid = ?`;
  const result = await exec(sql, [id, uid]);
  return result;
};

exports.editAddressByID = async function ({
  id,
  tel,
  nickname,
  provice_city_area,
  detail_address,
  longitude,
  latitude,
}) {
  const sql = `
    UPDATE address SET tel = ?, nickname = ?, provice_city_area = ?, detail_address = ?,longitude = ?,latitude = ? WHERE id = ?;
  `;
  return await exec(sql, [
    tel,
    nickname,
    provice_city_area,
    detail_address,
    longitude,
    latitude,
    id,
  ]);
};
